<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/**
 * Product list template
 *
 * @see \Magento\Catalog\Block\Product\ListProduct
 */
?>
<?php
$start = microtime(true);
$_productCollection = $block->getLoadedProductCollection();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
?>
<?php if (!$_productCollection->count()): ?>
<p class="message note"><?php /* @escapeNotVerified */ echo __('We can\'t find products matching the selection.') ?></p>
<?php else: ?>
<?php echo $block->getToolbarHtml() ?>
<?php echo $block->getAdditionalHtml() ?>
<?php
    if ($block->getMode() == 'grid') {
        $viewMode = 'grid';
        $image = 'category_page_grid';
        $showDescription = false;
        $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
    } else {
        $viewMode = 'list';
        $image = 'category_page_list';
        $showDescription = true;
        $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::FULL_VIEW;
    }
?>
<div class="products wrapper <?php /* @escapeNotVerified */ echo $viewMode; ?>">
    <?php $iterator = 1; ?>
    <ol class="products list items">
        <?php foreach ($_productCollection as $_product): ?>
        <?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="item product">' : '</li><li class="item product">' ?>
            <div class="product">
                <?php // Product Image ?>
                <a href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>" class="product photo">
                    <?php echo $block->getImage($_product, $image)->toHtml(); ?>
                </a>
                <div class="product details">
                <?php

                    $info = [];
                    $info['name'] = '<strong class="product name">'
                                    . ' <a href="' . $_product->getProductUrl() . '" title="'
                                    . $block->stripTags($_product->getName(), null, true) . '">'
                                    . $_helper->productAttribute($_product, $_product->getName(), 'name')
                                    . '</a></strong>';
                    $info['price'] = $block->getProductPrice($_product);
                    $info['review'] = $block->getReviewsSummaryHtml($_product, $templateType);

                    if ($_product->isSaleable()) {
                        $info['button'] = '<button type="button" title="' . __('Add to Cart') . '" class="action tocart"'
                                        . ' data-mage-init=\'{ "redirectUrl": { "event": "click", url: "' . $block->getAddToCartUrl($_product) . '"} }\'>'
                                        . '<span>' . __('Add to Cart') . '</span></button>';
                    } else {
                        $info['button'] = $_product->getIsSalable() ?   '<div class="stock available"><span>' . __('In stock') . '</span></div>' :
                                                                        '<div class="stock unavailable"><span>' . __('Out of stock') . '</span></div>';
                    }

                    $info['links'] = '<div class="product links" data-role="add-to-links">'
                                    . '<a href="#" data-post=\'' . $this->helper('Magento\Wishlist\Helper\Data')->getAddParams($_product) . '\' class="action towishlist" data-action="add-to-wishlist">'
                                    . '<span>' . __('Add to Wish List') . '</span></a>'
                                    . '<a href="' . $block->getAddToCompareUrl($_product) . '" class="action tocompare">'
                                    . '<span>' . __('Add to Compare') . '</span></a></div>';
                    $info['actions'] = '<div class="product action">' . $info['button'] . $info['links'] . '</div>';

                    if ($showDescription) {
                        $info['description'] =  '<div class="product description">'
                                                . $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description')
                                                . ' <a href="' . $_product->getProductUrl() . '" class="action more">'
                                                . __('Learn More') . '</a></div>';
                    } else {
                        $info['description'] = '';
                    }

                    $details = $block->getInfoOrder() ?: ['name','price','review','description','actions'];
                    foreach ($details as $detail) {
                        /* @escapeNotVerified */ echo $info[$detail];
                    }
                    ?>

                </div>
            </div>
        <?php echo($iterator == count($_productCollection)+1) ? '</li>' : '' ?>
        <?php endforeach; ?>
    </ol>
</div>
<?php echo $block->getToolbarHtml() ?>
<?php endif; ?>
<?php /* @escapeNotVerified */ echo $time_taken = microtime(true) - $start; ?>
